package part11;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

/**
 * @author wcj
 * @Date 2021/5/20 17:31
 * @Version 1.0
 */
public class Part117 {

    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9));
        Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()) {
            Integer next = iterator.next();
            System.out.println("元素:" + next);
        }
        System.out.println();
        for (Integer integer : list) {
            System.out.println("元素:" + integer);
        }
        System.out.println();
        iterator = list.iterator();
        while (iterator.hasNext()) {
            Integer next = iterator.next();
            iterator.remove();
        }
        System.out.println(list);
    }

    /**
     * 迭代器的强大之处就在于将遍历序列对于元素的处理操作和序列的底层类型分开，即采用迭代器遍历的时候我们从来没有关注过序列
     * 存放的什么类型的数据
     *
     * @param iterator
     */
    public static void display(Iterator<Integer> iterator) {
        while (iterator.hasNext()) {
            Integer next = iterator.next();
            System.out.println("元素:" + next);
        }
    }
}
